package cn.lyjuan.dwz_manage.core.java.cst;


import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * Created by chad on 2016/8/25.
 */
public class DwzManageCst
{
    /**
     * JDBC连接驱动，仅测试(spring profile为 dev和qa)时使用
     */
    public static String testJdbcDriver;

    /**
     * JDBC链接地址，仅测试时使用
     */
    public static String testJdbcUrl;

    /**
     * JDBC用户名，仅测试(spring profile为 dev和qa)时使用
     */
    public static String testJdbcUsername;

    /**
     * JDBC密码，仅测试(spring profile为 dev和qa)时使用
     */
    public static String testJdbcPassword;

    /**
     * 生产环境数据库JNDI
     */
    public static String jndiName;

    /**
     * 位于classpath下的MyBatis Mapper基础包，多个用英文逗号分隔<br />
     * 如：{@code mappers/base/mapper/,mappers/j2ee/mapper}
     */
    public static String[] mapperBasePackages;

    /**
     * HTTP后缀
     */
    public static String httpSuffix;

    /**
     * 来宾账户名称，关联来宾账户的请求所有人都可以访问（未登陆也可以）
     */
    public static String guestName;

    public void setMapperBasePackages(String mapperBasePackages)
    {
        if (StringUtils.isEmpty(mapperBasePackages)) return;
        this.mapperBasePackages = mapperBasePackages.split(",");
    }

    public void setHttpSuffix(String httpSuffix)
    {
        if (StringUtils.isEmpty(httpSuffix))
            throw new IllegalArgumentException("DwzManageCst httpSuffix can't be null");
        this.httpSuffix = httpSuffix.startsWith(".") ? httpSuffix : "." + httpSuffix;
    }

    public void setGuestName(String guestName)
    {
        if (StringUtils.isEmpty(guestName))
            guestName = "guest";
        this.guestName = guestName;
    }

    public DwzManageCst(Properties properties)
    {
        this.setTestJdbcDriver(properties.getProperty("testJdbcDriver"));
        this.setTestJdbcUrl(properties.getProperty("testJdbcUrl"));
        this.setTestJdbcUsername(properties.getProperty("testJdbcUsername"));
        this.setTestJdbcPassword(properties.getProperty("testJdbcPassword"));
        this.setJndiName(properties.getProperty("jndiName"));
        this.setMapperBasePackages(properties.getProperty("mapperBasePackages"));
        this.setHttpSuffix(properties.getProperty("httpSuffix", null));
        this.setGuestName(properties.getProperty("guestName", "guest"));
    }

    public void setTestJdbcDriver(String testJdbcDriver)
    {
        this.testJdbcDriver = testJdbcDriver;
    }

    public void setTestJdbcUrl(String testJdbcUrl)
    {
        this.testJdbcUrl = testJdbcUrl;
    }

    public void setTestJdbcUsername(String testJdbcUsername)
    {
        this.testJdbcUsername = testJdbcUsername;
    }

    public void setTestJdbcPassword(String testJdbcPassword)
    {
        this.testJdbcPassword = testJdbcPassword;
    }

    public void setJndiName(String jndiName)
    {
        this.jndiName = jndiName;
    }

    public String getTestJdbcDriver()
    {
        return testJdbcDriver;
    }

    public String[] getMapperBasePackages()
    {
        return mapperBasePackages;
    }

    public String getHttpSuffix()
    {
        return httpSuffix;
    }

    public String getJndiName()
    {
        return jndiName;
    }

    public String getTestJdbcPassword()
    {
        return testJdbcPassword;
    }

    public String getTestJdbcUsername()
    {
        return testJdbcUsername;
    }

    public String getTestJdbcUrl()
    {
        return testJdbcUrl;
    }

    public static String getGuestName()
    {
        return guestName;
    }
}
